<!--头部公共部分-->
{include file="common/header" /}
<style>
    input::-webkit-outer-spin-button,
    input::-webkit-inner-spin-button {
        -webkit-appearance: none;
    }
    input[type="number"] {
        -moz-appearance: textfield;
    }
</style>

<div class="x-nav">
      <span class="layui-breadcrumb">
        <a><cite>新客户新增</cite></a>
      </span>
    <a class="layui-btn layui-btn-xs layui-btn-small" style="margin-top:7px;float:right;margin-left: 10px;"
       href="{:url('Customer/lists')}">
        返回我的客户</a>

</div>
<div style="padding:15px;">
    <div class="layui-tab">
        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
            <legend>基本信息</legend>
        </fieldset>
        <div class="layui-tab-content">
            <div class="layui-tab-item layui-show">
                <form class="layui-form" style="width: 970px;">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label"><span class="x-red">*</span>书店名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="name" lay-verify="required" placeholder="请输入书店名称" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label"><span class="x-red">*</span>联系方式</label>
                            <div class="layui-input-inline">
                                <input type="number" name="phone" lay-verify="required" placeholder="请输入联系方式" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">联系人</label>
                            <div class="layui-input-inline">
                                <input type="text" name="customer_name" placeholder="联系人" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">省/市/区</label>
                            <div class="layui-input-inline">
                                <input type="text" name="address" placeholder="请输入省/市/区" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">来源渠道</label>
                            <div class="layui-input-inline">
                                <select name="channel" lay-search="" lay-filter="channel">
                                    <option value="">请选渠道</option>
                                    {volist name="data[2]" id="channel"}
                                    <option value="{$channel.id}">{$channel['name']}</option>
                                    {/volist}
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">成熟度</label>
                            <div class="layui-input-inline">
                                <select name="intentional_grade" lay-search="" lay-filter="intentional_grade">
                                    <option value="">请选成熟度</option>
                                    <option value="0">0类</option>
                                    <option value="1">1类</option>
                                    <option value="2">2类</option>
                                    <option value="3">3类</option>
                                    <option value="4">4类</option>
                                    <option value="5">5类</option>
                                </select>
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label"><span class="x-red">*</span>跟进人</label>
                            <div class="layui-input-inline">
                                {if $current_type == 5}
                                <select name="user_id" lay-filter="user_id" disabled>
                                    {volist name="users" id="user"}
                                    <option value="{$user.id}">{$user['name']}</option>
                                    {/volist}
                                </select>
                                {else/}
                                <select name="user_id" lay-search="" lay-verify="required"  lay-filter="user_id">
                                    <option value="">请选跟进人</option>
                                    {volist name="users" id="user"}
                                    <option value="{$user.id}">{$user['name']}</option>
                                    {/volist}
                                </select>
                                {/if}
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">手机号码</label>
                            <div class="layui-input-inline">
                                <input type="number" name="guardian_phone" placeholder="请输入手机号码" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">电话号码</label>
                            <div class="layui-input-inline">
                                <input type="number" name="other_phone" placeholder="请输入电话号码" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">更多联系人</label>
                            <div class="layui-input-inline">
                                <input type="number" name="other" placeholder="请输入更多联系人" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">微信账号</label>
                            <div class="layui-input-inline">
                                <input type="text" name="weixin" placeholder="请输入微信" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">QQ账号</label>
                            <div class="layui-input-inline">
                                <input type="text" name="qq" placeholder="请输入QQ" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item layui-form-text">
                        <label class="layui-form-label">备注信息</label>
                        <div class="layui-input-block">
                            <textarea name="remark" placeholder="请输入备注信息" class="layui-textarea"></textarea>
                        </div>
                    </div>
                    <div class="layui-form-item layui-form-text">
                        <label class="layui-form-label">详细地址</label>
                        <div class="layui-input-block">
                            <textarea name="detailed_address" placeholder="请输入详细地址" class="layui-textarea"></textarea>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-input-block">
                            <a class="layui-btn layui-btn-normal" lay-submit lay-filter="form" readonly>保存</a>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
<!--底部公共部分-->
{include file="common/footer" /}
<script>
    layui.config({
        base: '/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'rate','element', 'jquery','form','laydate','layer'], function(){
        let $ = layui.$,
                rate = layui.rate,
                layer = layui.layer,
                form = layui.form,
                element = layui.element,
                laydate = layui.laydate,
                jquery = layui.jquery;

        //监听提交
        form.on('submit(form)', function (data) {
            // console.log(data);
            //发异步，把数据提交给php
            let field = data.field;
            field.channel = $('select[name=channel] option:selected').val();
            field.other_phone = $('input[name=other_phone]').val();
            field.other = $('input[name=other]').val();
            field.guardian_phone = $('input[name=guardian_phone]').val();
            if(field.other_phone !== ''){
                if((field.other_phone).length !== 11){
                    layer.msg('手机号码必须11位，且不能出现空格', {icon: 7, time: 2000}, function () {
                    });
                    return false;
                }
            }
            if(field.guardian_phone !== ''){
                if((field.guardian_phone).length !== 11){
                    layer.msg('手机号码必须11位，且不能出现空格', {icon: 7, time: 2000}, function () {
                    });
                    return false;
                }
            }
            field.type = 2;
            let isLoad;
            $.ajax({
                url: '{:url("Customer/add")}',
                method: "post",
                data: field,
                beforeSend: function () {
                    isLoad = layer.load(0, {
                        shade: false
                    }); //0代表加载的风格，支持0-2
                },
                success: function (result) {
                    if(result.code === 1) {
                        layer.msg(result.msg, {icon: 1, time: result.wait * 1000},function () {
                            location.href = '{:url("Customer/lists")}';
                        });
                    }else {
                        layer.msg(result.msg, {icon: 2, time: result.wait * 1000},function () {
                            // location.reload();
                        });
                    }
                },
                complete: function () {
                    layer.close(isLoad)
                },
                error: function (error) {
                    console.log(error)
                }
            });
            return false;
        });
    });
</script>